package com.sushe.dao;

import java.sql.*;

public class RegisterDao {
    private static final String url="jdbc:mysql://localhost:3306/sushe";
    private static final String username="root";
    private static final String password="123456";

    public boolean select(String userName) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        String sql="select id from user where username=?";
        Connection connection= DriverManager.getConnection(url, username, password);
        PreparedStatement stmt=connection.prepareStatement(sql);
        stmt.setString(1,userName);
        ResultSet rs=stmt.executeQuery();
        while(rs.next()) {
            Integer Id = rs.getInt("id");
            if(Id!=null)
                return true;
        }
        return false;
    }

    public void addUser(String userName,String passWord) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        //  获取连接
        String sql="insert into user (username,password) values(?,?)";
        Connection connection= DriverManager.getConnection(url,username,password);
        //  创建statement
        PreparedStatement stmt=connection.prepareStatement(sql);
        stmt.setString(1,userName);
        stmt.setString(2,passWord);
        int i=stmt.executeUpdate();
        System.out.println("SQL语句执行完毕影响的记录数为"+i);
        stmt.close();
        connection.close();
    }
}
